#include <stdio.h>
#include <stdlib.h>
#include "list.h"  //双向循环链表

/**
 * 使用list_for_each_entry_safe 代替list_for_each_entry  解决list_del重复删除问题
 */

//自定义的类型
struct student {
    int sno;
    struct list_head list; //节点类型
};


int main(void)
{
    int i;
    struct student *p;
    struct student head; //头节点
    struct student *pos, *n; //保存遍历链表节点的指针
    //初始化内核链表
    INIT_LIST_HEAD(&head.list); //内核链表头节点的指针

    for (i = 1; i <= 5; i++) {
        p = (struct student *)malloc(sizeof(struct student));
        p->sno = i *10;
        list_add(&p->list, &head.list); //头节点后面插入
        if (i == 2) {
            p = (struct student *)malloc(sizeof(struct student));
            p->sno = i *10;
            list_add(&p->list, &head.list); //头节点后面插入
        }
    }
    printf("head");
    list_for_each_entry_safe(pos, n, &head.list, list) { 
        printf("<->%d", pos->sno);

    }
    printf("<->head\n");
    printf("=============del=================\n");
    // list_for_each_entry(pos, &head.list, list) { 
    list_for_each_entry_safe(pos, n, &head.list, list) { 
        if (pos->sno == 20)  {
            list_del(&pos->list);
           
        }
    }
    printf("head");
    list_for_each_entry_safe(pos, n, &head.list, list) {
        printf("<->%d", pos->sno);

    }
    printf("<->head\n");

}




